<?php

class Application_Model_UserAuth
{
    protected $_table=Application_Model_User::DB_TABLE;
    protected $_adapter;
    
    protected $_login;
    protected $_pwd_hash;
    
    public function __construct()
    {
        $this->_restoreSession();
    }
    
    /**
     * Sets adapter for user authentication
     *
     */
    protected function _setAdapter()
    {
        $this->_adapter=new Zend_Auth_Adapter_DbTable(
                    null,
                    $this->_table,
                    'login',
                    'password'
                );
    }
    
    /**
     * Checks user credentials
     *
     * @param string $login
     * @param string $password
     * @return bool
     */
    protected function _checkAuth($login,$password)
    {
        $this->_adapter->setIdentity($login);
        $this->_adapter->setCredential($password);
        
        $result=$this->_adapter->authenticate();
        
        if ($result->isValid())
        {
            $this->_login=$login;
            $this->_pwd_hash=$password;
            
            return true;
        }
        
        return false;
    }
    
    /**
     * Authenticates user
     *
     * @param string $login
     * @param string $password
     * @return bool
     */
    public function authenticate($login,$password)
    {
        $this->_setAdapter();
        
        $password=Application_Model_User::crypt_password($password);
        
        return $this->_checkAuth($login,$password);
    }
    
    /**
     * Saves user credentials in user session
     *
     */
    public function saveUserSession()
    {
        if (empty($this->_login) || empty($this->_pwd_hash)) return;
        
        $session=new Zend_Session_Namespace('userAuth');
        
        $session->login=$this->_login;
        $session->password=$this->_pwd_hash;
        
        $session->lock();
    }
    
    /**
     * Restores user session
     *
     */
    protected function _restoreSession()
    {
        $session=new Zend_Session_Namespace('userAuth');
        
        if (!empty($session->login) && !empty($session->password))
        {
            $this->_setAdapter();
            
            if (!$this->_checkAuth($session->login,$session->password))
            {
                $this->destroyUserSession();
            }
        }
    }
    
    /**
     * Destroys user session
     *
     */
    public function destroyUserSession()
    {
        $this->_login=null;
        $this->_pwd_hash=null;
        
        $session=new Zend_Session_Namespace('userAuth');
        
        $session->unLock();
        
        unset($session->login,$session->password);
    }
    
    /**
     * Returns authenticated user login
     *
     * @return string
     */
    public function getAuthUserLogin()
    {
        return $this->_login;
    }
    
    /**
     * Checks weather user is already authenticated
     *
     * @return bool
     */
    public function isAuthenticated()
    {
        return !empty($this->_login) ? true : false;
    }
}

